<?php
namespace Admin\Controller;

use Think\Exception;
use Think\Model;

/**
 * Class 店铺可售货品
 * @package Admin\Controller
 */
class StoreGoodsSalesController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    public function goodsSale()
    {

        $p = max(I('param.p'), 1);
        $num = max(I('param.num'), 10);

        $start_time = I('start_time', '');
        $end_time = I('end_time', '');

        $where = [
            ['local_id' => $this->local_id],
            ['a.time' => ['EGT', $start_time]],
            ['a.time' => ['LT', $end_time]],
        ];
        if (!empty($start_time) && !empty($end_time)) {
            $_GET['p'] = 1;
            $where = " local_id = ".$this->local_id." and time >='".$start_time."' and time <= '".$end_time."'";
        } else {
            $where = " local_id = ".$this->local_id;
        }
        $list = M()->query("SELECT *,SUM(goods_num) as sum_num,SUM(money) as sum_total FROM `sdb_statistical_sales_goods` WHERE $where GROUP BY time,store_pid order by time desc");
        $data = [];
        foreach ($list as &$item) {
            $item['time'] = strtotime($item['time']);
            $item['time'] = date('Y-m-d',$item['time']);
            $data[$item['time']][] = $item;
        }


        $Page = new \Think\Page(count($data), $num);
        $show = $Page->show();
        if (empty($_GET['p'])) {
            $_GET['p'] = 1;
        }
        $start = ($_GET['p'] - 1) * $num;

        $data_new = array_slice($data, $start, $num);

        $this->assign('total', $count);
        $this->assign('list', $data_new);
        $this->assign('page', $show);


        $this->display('SaleCount/goodsSales');
    }

    public function goodsSalesExport()
    {


        $start_time = I('hidden-start-time', '');
        $end_time = I('hidden-end-time', '');



        if (!empty($start_time) && !empty($end_time) && ( $start_time!='开始时间' && $start_time!='结束时间')) {
            $where = " local_id = ".$this->local_id." and time >='".$start_time."' and time <= '".$end_time."'";
        } else {
           
            $where = " local_id = ".$this->local_id;
        }


        $list = M()->query("SELECT time,barcode,bn,goods_name,SUM(goods_num) as sum_num,SUM(money) as sum_total FROM `sdb_statistical_sales_goods` WHERE $where GROUP BY time,store_pid order by time desc");

        $data = [];
        foreach ($list as &$item) {
            $item['time'] = strtotime($item['time']);
            $item['time'] = date('Y-m-d',$item['time']);

            $data[$item['time']][] = $item;



        }

        $title = array(
            array('time', '日期'),
            array('barcode', '条码'),
            array('bn', '商品编码'),
            array('goods_name', '商品名'),
            array('sum_num', '销售商品数量'),
            array('sum_total', '销售金额/元')
        );

        $this->exportexcel('商品销售统计', $title, $data);


    }

    public function exportexcel($expTitle, $expCellName, $expTableData)
    {
        //print_r($expTableData);
        $data_time = date('Y-m-d', time());
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $data_time . $expTitle;//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle . '  Export time:' . date('Y-m-d H:i:s'));
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
        }


        $m = 3;
        foreach ($expTableData as $key => $value) {
            $is_count = count($value);
            //print_r($value);
            $objPHPExcel->getActiveSheet()->mergeCells('A' . $m . ':A' . ($m + $is_count - 1));
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $m, $key);

            for ($i = 0; $i < $is_count; $i++) {
                for ($j = 0; $j < $cellNum; $j++) {
                    $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j + 1] . ($m), $value[$i][$expCellName[$j + 1][0]]);
                    /* echo $cellName[$j+1].($m).'--'.$value[$i][$expCellName[$j+1][0]];
                   echo '<br>';*/
                }
                $m++;
            }

        }

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;


    }


}